package io.lkt.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.Query;
import io.lkt.modules.sys.dao.BTeamRelationDao;
import io.lkt.modules.sys.entity.BTeamRelationEntity;
import io.lkt.modules.sys.service.BTeamRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("bTeamRelationService")
public class BTeamRelationServiceImpl extends ServiceImpl<BTeamRelationDao, BTeamRelationEntity> implements BTeamRelationService {

    @Autowired
    BTeamRelationDao bTeamRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BTeamRelationEntity> page = this.page(
                new Query<BTeamRelationEntity>().getPage(params),
                new QueryWrapper<BTeamRelationEntity>()
                        .eq("user_id", params.get("userId"))
                        .eq("user_flag", 1)
                        .orderByDesc("id")
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageForTeamId(Map<String, Object> params) {
        IPage<BTeamRelationEntity> page = this.page(
                new Query<BTeamRelationEntity>().getPage(params),
                new QueryWrapper<BTeamRelationEntity>()
                        .eq("team_id", params.get("teamId"))
                        .eq("user_flag", 1)
                        .orderByDesc("id")
        );

        return new PageUtils(page);
    }

    @Override
    public Integer queryTotal(Map<String, Object> map) {

        QueryWrapper<BTeamRelationEntity> queryWrapper = new QueryWrapper();

        queryWrapper.eq("team_id", map.get("teamId"))
                .eq("user_flag", 1);

        List<BTeamRelationEntity> temporary = bTeamRelationDao.selectList(queryWrapper);
        return temporary.size();
    }

    @Override
    public BTeamRelationEntity queryCreateTime(Map<String, Object> map) {

        QueryWrapper<BTeamRelationEntity> queryWrapper = new QueryWrapper();

        queryWrapper.eq("team_id", map.get("teamId"))
                .eq("team_role", 2)
                .eq("user_flag", 1);

        BTeamRelationEntity temporary = bTeamRelationDao.selectOne(queryWrapper);
        return temporary;
    }

    @Override
    public int getUserTeamCount(Integer userId) {
        return bTeamRelationDao.getUserTeamCount(userId);
    }

}
